# !/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2023/9/8 15:29
# @Author  : xinyingjie
# @File    : main-dev.py
# @Description :  速通高速ETC断面高速仿真发车


from __future__ import absolute_import
from __future__ import print_function

import json
import os
import threading
import uuid
import random
import time
import asyncio
import traci.constants as tc
import sumolib
import traci
from util import sumo_utils, kafka_util
from util.kafka_util import real_vehicle_queue
from util.websocketserver1 import WebSocketServer
import logging

# 全局中心点6323.78,31009.31  12320.04,30723.05  5362.89,27538.55  13320.35,27643.33
GLOBAL_POLYGON_CENTER = [(2156.62, 2207.91), (2237.31, 2204.92),
                         (2248.41, 2087.94), (2119.47, 2089.22)]
# 异常标签
# unlawfulMap = {"多卡多签": "0101", "车型异常": "0102", "ETC无入口信息": "0103", "疑似屏蔽": "0104", "其他异常": "0104", "无号牌": "0105"}
unlawfulMap = {"0101": "0101", "0102": "0102", "0103": "0103"}



def get_logger(log_type):
    # 第一步，创建一个logger
    logger = logging.getLogger()
    logger.setLevel(logging.INFO)  # Log等级总开关
    # 第二步，创建一个handler，用于写入日志文件
    rq = str(time.strftime('%Y%m%d', time.localtime(time.time()))) + '_' + str(log_type)
    log_path = 'logs/'
    # log_path = '/opt/python' + '/logs/'
    # print('log_path', log_path)
    if not os.path.exists(log_path):
        os.mkdir(log_path)

    log_name = log_path + rq + '.log'
    logfile = log_name
    fh = logging.FileHandler(logfile)
    fh.setLevel(logging.DEBUG)  # 输出到file的log等级的开关
    # 第三步，定义handler的输出格式
    formatter = logging.Formatter("%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s")
    fh.setFormatter(formatter)
    # 第四步，将logger添加到handler里面
    logger.addHandler(fh)
    return logger

















def main(sumo_cfg_file=None, sumo_net_file=None, gui=True):
    sumo_net_instance = sumolib.net.readNet(sumo_net_file, withInternal=True, withLatestPrograms=True)
    sumo_utils.start_sumo(sumo_cfg_file, False, gui=True)
    # 设置所有车道的最大通行速度
    for id in traci.lane.getIDList():
        traci.lane.setMaxSpeed(id, 50)
    traci.polygon.add('GLOBAL_POLYGON_CENTER', GLOBAL_POLYGON_CENTER, (1, 0, 1))
    traci.polygon.subscribeContext('GLOBAL_POLYGON_CENTER',
                                   tc.CMD_GET_VEHICLE_VARIABLE, 10000,
                                   [tc.VAR_SPEED, tc.VAR_DECEL, tc.VAR_DISTANCE, tc.VAR_TYPE, tc.VAR_POSITION,
                                    tc.VAR_ROAD_ID, tc.VAR_LANE_INDEX, tc.VAR_LANE_ID, tc.VAR_ACCEL, tc.VAR_ANGLE,
                                    tc.VAR_LANE_ID, tc.VAR_ACCELERATION, tc.VAR_LANE_INDEX,
                                    tc.TRAFFICLIGHT_TYPE_STATIC])
    traci.simulation.subscribe([tc.VAR_ARRIVED_VEHICLES_NUMBER,
                                tc.VAR_ARRIVED_VEHICLES_IDS,
                                tc.VAR_DEPARTED_VEHICLES_NUMBER,
                                tc.VAR_DEPARTED_VEHICLES_IDS,
                                tc.VAR_COLLIDING_VEHICLES_NUMBER,
                                tc.VAR_COLLIDING_VEHICLES_IDS])
    # 获取虚拟设备状态kafka数据
    # vir_thread = threading.Thread(target=kafka_util.receive_message_of_ETC_vehicle)
    # vir_thread.start()
    # ws_server = WebSocketServer()
    # ws_server.start()

    EdgeID, Pos, LaneIndex = traci.simulation.convertRoad(116.1922105774, 39.7909376219, True)
    EdgeID1, Pos1, LaneIndex1 = traci.simulation.convertRoad(116.1921713501215, 39.7909468963, True)
    EdgeID2, Pos2, LaneIndex2 = traci.simulation.convertRoad(116.192129440, 39.79095024537, True)

    print(EdgeID,LaneIndex)
    print(EdgeID1, LaneIndex1)
    print(EdgeID2, LaneIndex2)


    routeID = str(uuid.uuid1())
    traci.route.add(routeID, [EdgeID])
    routeID1 = str(uuid.uuid1())
    traci.route.add(routeID1, [EdgeID1])
    routeID2 = str(uuid.uuid1())
    traci.route.add(routeID2, [EdgeID2])

    # print(EdgeID, Pos, LaneIndex)
    # print(EdgeID1, Pos1, LaneIndex1)
    # print(EdgeID2, Pos2, LaneIndex2)

    posDict = {"1": {"pos": Pos, "route": routeID}, "2": {"pos": Pos1, "route": routeID1},
               "3": {"pos": Pos2, "route": routeID2}}
    logger = get_logger("simulation")
    # f = open("data//data0908v1.json", encoding='utf-8')
    try:
        # f = open("../data//data0908v1.json", encoding='utf-8')
        loop = asyncio.get_event_loop()
        while (True):
            try:
                # line = f.readline().strip()
                # if line:
                if True:
                    # vehiclePicInfo = json.loads(line)
                    vehiclePicInfo = {
                        "dangerousGoods": "",
                        "devType": "4,5",
                        "driveDir": 1,
                        "erId": "G006952006005010010",
                        "etcContractSerial": "3301016100735263",
                        "etcEntryAxles": "2",
                        "etcEntryClass": "21",
                        "etcEntryTime": "2023-11-15 16:28:46",
                        "etcGeneralState": "1",
                        "etcLicense": "京MMS157",
                        "etcLicenseColor": "0",
                        "etcMac": "A1A11A56",
                        "etcMediaType": "CPC",
                        "etcStatus": "0000",
                        "etcTollStation": "1101080543",
                        "etcTradeResult": "0",
                        "etcTransTime": "2023-11-15 16:28:36",
                        "etcValidSign": "00",
                        "etcVehClass": "21",
                        "etcVehUserType": "0",
                        "exitVehicleSpeed": 0,
                        "headImage": "http://192.168.0.50:8809/G00695200600501001020231115162922819041836/0.jpeg",
                        "headLaneNum": "2",
                        "headPicTime": "2023-11-15 16:28:51",
                        "hourBatchNo": "2023111516",
                        "identifyType": 21,
                        "licenseAbbre": "京",
                        "licenseCode": "京MMS157",
                        "licenseColor": "0",
                        "licenseImage": "http://192.168.0.50:8809/G00695200600501001020231115162922819041836/3.jpeg",
                        "recordId": "G00695200600501001020231115162922819041836",
                        "recordType": "B",
                        "relateEtcContractSerial": "",
                        "relateEtcCount": "0",
                        "relateEtcMac": "",
                        "relateEtcMediaType": "",
                        "sideImage": "http://192.168.0.50:8809/G00695200600501001020231115162922819041836/2.jpeg",
                        "sideLaneNum": "2",
                        "sidePicTime": "2023-11-15 16:28:52",
                        "tailImage": "http://192.168.0.50:8809/G00695200600501001020231115162922819041836/1.jpeg",
                        "tailLaneNum": "2",
                        "tailPicTime": "2023-11-15 16:28:51",
                        "unlawfulMold": "",
                        "vehTradeResult": "0",
                        "vehicleAxleCount": 2,
                        "vehicleAxleInfo": "12",
                        "vehicleHeight": 0,
                        "vehicleLength": 619,
                        "vehicleSpeed": 132,
                        "vehicleWidth": 196,
                        "video": "http://192.168.0.50:8809/G00695200600501001020231115162922819041836/4.mp4"
                    }

                    # if line:
                    #     vehiclePicInfo = json.loads(line)
                    unlawfulMold = vehiclePicInfo["unlawfulMold"]
                    dg = vehiclePicInfo["dangerousGoods"]
                    if "危化" in str(dg):
                   # if int(dg) == 1:
                        unlawfulMold = "0103"
                    if vehiclePicInfo["identifyType"] == 3 or vehiclePicInfo["identifyType"] == 4:
                        unlawfulMold = "0104"
                    # 1 小客车   3 大巴车 15 大货车
                    if 1 <= vehiclePicInfo['identifyType'] <= 2:
                        typeid = '1'
                    elif 11 <= vehiclePicInfo['identifyType'] <= 26:
                        typeid = '15'
                    elif 3 <= vehiclePicInfo['identifyType'] <= 4:
                        typeid = '3'
                    licenseCode = ""
                    if "默" not in vehiclePicInfo["licenseCode"]:
                        licenseCode = vehiclePicInfo["licenseCode"]
                    else:
                        if "etcLicense" in vehiclePicInfo.keys() and  len(vehiclePicInfo["etcLicense"]) > 0:
                            licenseCode = vehiclePicInfo["etcLicense"]
                    if len(licenseCode) > 0:
                        if len(unlawfulMold) > 0:
                            traci.vehicle.add(
                                vehID=str(time.time()),
                                routeID=posDict[vehiclePicInfo['headLaneNum']]["route"],
                                typeID=typeid, depart='0', departPos=posDict[vehiclePicInfo['headLaneNum']]["pos"],
                                departLane=str(int(vehiclePicInfo['headLaneNum']) - 1)
                                if 'headLaneNum' in vehiclePicInfo.keys() else str(random.randint(0, 2)),
                                departSpeed=str(vehiclePicInfo['vehicleSpeed'] / 3.6))
                        else:
                            traci.vehicle.add(
                                vehID=str(time.time()),
                                routeID=posDict[vehiclePicInfo['headLaneNum']]["route"],
                                typeID=typeid, depart='0', departPos=posDict[vehiclePicInfo['headLaneNum']]["pos"],
                                departLane=str(int(vehiclePicInfo['headLaneNum']) - 1)
                                if 'headLaneNum' in vehiclePicInfo.keys() else str(random.randint(0, 2)),
                                departSpeed=str(vehiclePicInfo['vehicleSpeed'] / 3.6))

                global_vehicles = traci.polygon.getContextSubscriptionResults('GLOBAL_POLYGON_CENTER')
                car_list = list()
                # print(len(global_vehicles))
                for vehicle_id, veh_info in dict(global_vehicles).items():
                    if True:
                        pos = veh_info[tc.VAR_POSITION]
                        longitude, latitude = sumo_utils.xyConversionlatAndLon(sumo_net_instance, pos)
                        speed = veh_info[tc.VAR_SPEED]
                        vehicle_type = veh_info[tc.VAR_TYPE]
                        info = vehicle_id.split("_")
                        unlawfulMold = ""

                        vId = info[1]
                        licenseCode = info[0]
                        if vehicle_type == "3":
                            vehicle_type = "6"
                        if vehicle_type == "15":
                            vehicle_type = "3"
                        if len(info) == 3:
                            unlawfulMold = info[2]
                            if unlawfulMold == "0103":
                                vehicle_type = "8"
                        if (unlawfulMold[:4] == "0102" or unlawfulMold[:4] == "0103") and latitude < 39.796576:
                            unlawfulMold = ""
                        if unlawfulMold == "0104":
                            unlawfulMold = "0103"
                        car = {"id": vId, "vehicleType": vehicle_type, "longitude": longitude,
                               "latitude": latitude,
                               "courseAngle": veh_info[tc.VAR_ANGLE], "acceleration": veh_info[tc.VAR_ACCELERATION],
                               "speed": round(speed * 3.6, 2), "laneNum": veh_info[tc.VAR_LANE_INDEX],
                               "licenseode": eval(licenseCode).decode('utf-8'),
                               "unlawfulMold": unlawfulMold,
                               "unlawfulCode": unlawfulMap[unlawfulMold[:4]] if unlawfulMold[:4] in unlawfulMap.keys() else "",
                               }
                        
                        car_list.append(car)

                result = {"e1FrameParticipant": car_list, "participantNum": len(car_list),
                          "timeStamp": str(time.time()),
                          "globalTimeStamp": str(time.time())}
                logger.info(result)
                loop.run_until_complete(ws_server.broadcast(str(result).replace("'", '"')))
                # websocketserver.test("1111")
                traci.simulationStep()
                time.sleep(0.05)
            except Exception as e:
                traci.simulationStep()
                time.sleep(0.05)
                logging.exception(str(e))
                continue

    except Exception as e:
        logging.exception(str(e))


if __name__ == "__main__":
    sumo_net_file = r"D:\工作\项目\速通高速\fengtai_etc_sumo-image\config\0815-v1.net.xml"  # 路网文件
    sumo_cfg_file = r"D:\工作\项目\速通高速\fengtai_etc_sumo-image\config\fengtai.sumocfg"  # 路网文件
    logger = get_logger("simulation")
    logger.info("test")

    main(sumo_cfg_file=sumo_cfg_file, sumo_net_file=sumo_net_file)
    # sumo_net_file = "../config/0815-v1.net.xml"  # 路网文件
    # sumo_cfg_file = "../config/fengtai.sumocfg"  # 路网文件
    # main(sumo_cfg_file=sumo_cfg_file, sumo_net_file=sumo_net_file)
